// formPackage/pages/invite/inviteDetail/inviteDetail.js

import { queryInviteDetail, queryInviteQrCode } from "../../../../service/invite_service"
import { loginWxAction } from "../../../../service/config"
import { bindUserRelation } from "../../../../service/user_service"
import debounce from "../../../../tools/debounce"
import { MAP_KEY } from "../../../../service/base_service"
let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    //地图key -- 地图相关
    mapSubkey: MAP_KEY,
    latitude: 30.690885,
    longitude: 104.078367,
    enable3d: false,
    showCompass: false,
    enableOverlooking: false,
    enableZoom: false,
    enableScroll: false,
    enableRotate: false,
    drawPolygon: false,
    enableSatellite: false,
    enableTraffic: false,
    detailId: null,
    formData: {},
    bindUserId: "",//绑定关系的userId,
    isScanEnter: false,//是否是扫码进来的
    isShare: false,//是否分享进来的

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        detailId: options.id,
      })
    }
    //扫码进来的
    if (options.scene) {
      let scene = decodeURIComponent(options.scene)
      console.log("JJLog:9999999===:" + scene);
      let sceneArray = scene.split("&");
      let firstArr = sceneArray[0].split("=");
      let secondArr = sceneArray[1].split("=");
      let threeArr = sceneArray[2].split("=");
      let b = secondArr[1];
      let detailId = firstArr[1];
      if (b == 1) {
        this.setData({
          isScanEnter: true
        })
      }
      let userId = threeArr[1];
      this.setData({
        bindUserId: userId,
        isShare: true,
        detailId: detailId
      })

      this.userWxLogin();

      return;
    }
    if (options.isShare) {
      this.setData({
        isShare: true,
        isScanEnter: false,
        detailId: options.id,
        bindUserId: options.userId,
      })
      this.userWxLogin();
      return;
    }


    this.queryInviteDetail();
    this.queryInviteQrCode();
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  queryInviteDetail() {
    wx.showLoading();
    queryInviteDetail(this.data.detailId).then(res => {
      wx.hideLoading();
      console.log("JJLog:回填信息是:" + JSON.stringify(res));
      if (res.code == 200) {
        if (res.data) {
          this.setData({
            formData: res.data,
            latitude: res.data.latitude,
            longitude: res.data.longitude
          })
        }
      }
    }).catch(err => {
      console.error("JJLog:queryInviteDetail", err);
      wx.hideLoading();
    })
  },
  //查询对应二维码
  queryInviteQrCode() {
    queryInviteQrCode(this.data.detailId).then(res => {
      console.log("JJLog:招募令二维码:" + JSON.stringify(res));
      if (res.code == 200) {
        app.globalData.inviteQrCode = res.data || ""
      }
    }).catch(err => {
      console.log("JJLog:queryInviteQrCode", err);
      app.globalData.inviteQrCode = "";
    })
  },
  //用户登陆授权等:
  userWxLogin() {
    loginWxAction().then((res => {
      console.log("JJLog:登录返回:" + JSON.stringify(res));
      let data = res.data.data;
      if (res.code == 200) {
        app.globalData.nickName = data.nickName;
        app.globalData.avatar = data.avatar;
        app.globalData.name = data.name || "";
        app.globalData.phone = data.phone || "";
        app.globalData.isLogin = data.isLogin;
        app.globalData.userId = data.id;
        app.globalData.qrCode = data.qrCode || "";
        app.globalData.isAuthUserInfo = data.isAuthUserInfo;
        app.globalData.weChat = data.weChat || "";
        app.globalData.roleName = data.roleName || "";
        app.globalData.companyName = data.companyName || "";
        app.globalData.companyNumber = data.companyNumber || "";
        //绑定挂级关系:
        if (this.data.bindUserId) {
          this.bindUserRelation();
        }
        this.queryInviteDetail();
        this.queryInviteQrCode();
      }
    })).catch(err => {
      console.error("JJLog:userWxLogin", err);
    })
  },
  //绑定挂级关系:
  bindUserRelation() {
    let type = 1;
    if (this.data.isScanEnter) {
      type = 1;
    } else {
      type = 2;
    }
    bindUserRelation(type, this.data.bindUserId).then(res => {
      console.log("JJLog:绑定挂级关系:" + JSON.stringify(res));

    }).catch(err => {
      console.error("JJLog:bindUserRelation:", err);
    })
  },

  //跳转到对应地址:
  pushToAddress() {
    wx.openLocation({
      latitude: this.data.formData.latitude,	//维度
      longitude: this.data.formData.longitude, //经度
      name: "",	//目的地定位名称
      scale: 15,	//缩放比例
      address: this.data.formData.address	//导航详细地址
    })

  },
  callPhoneAction(e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
      fail: err => {

      }
    })
  },

  //跳转到聊天:
  pushToChat() {
    if (!app.globalData.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    let item = this.data.formData;
    let chatData = {
      name: item.name || item.nickName,
      avatar: item.avatar || "",
      chatUserId: item.openId,
      conversationType: "C2C",
      houseNumber: "",
      houseName: "",
      expertId: item.id,
      expertPhone: item.phone || "",
      expertWeChat: item.wxChat || "",
    }
    app.globalData.chatData = chatData;
    wx.navigateTo({
      url: '/firstPackage/pages/msgModule/chatRoom/chatRoom',
    })
  },
  //招聘海报分享
  sharePosterAction() {
    if (!app.globalData.inviteQrCode) {
      wx.showToast({
        title: '暂无无法分享招募信息',
        icon: "none"
      })
      return;
    }
    let data = this.data.formData;
    let obj = {
      jobName: data.jobName,
      id: this.data.detailId,
      salary: data.salary,
      companyName: data.companyName,
      contactUser: data.contactUser,
      contactPhone: data.contactPhone,
      avatar: data.avatar || "",
      qrCode: app.globalData.inviteQrCode,
    }
    app.globalData.posterInvite = obj;
    wx.navigateTo({
      url: '/newHousePackage/pages/housePoster/housePoster?type=invite',
    })
  },

  backHomeAction() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    let path = "";
    path = "/formPackage/pages/invite/inviteDetail/inviteDetail?isShare=1" + "&userId=" + app.globalData.userId + "&id=" + this.data.detailId

    return {
      title: (this.data.formData.companyName + '招聘' + this.data.formData.jobName) || app.globalData.commonShareTitle,
      path: path
    }
  },




})